package com.fqgj.sentry.controller.mc.response;

import com.fqgj.sentry.manage.domain.PermissionMenuInfo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liupengpeng on 2017/7/31.
 */
public class PermissionMenuResponse implements Serializable {
    private List<Menu> list;

    public List<Menu> getList() {
        return list;
    }

    public PermissionMenuResponse setList(List<Menu> list) {
        this.list = list;
        return this;
    }

    public PermissionMenuResponse(List<PermissionMenuInfo> permissionMenuInfoList) {
        Map<Long, Menu> menus = new HashMap<>();
        for (PermissionMenuInfo permissionMenuInfo : permissionMenuInfoList) {
            Menu menu = menus.get(permissionMenuInfo.getPid());
            if (menu == null) {
                Menu parentMenu = new Menu();
                ChildMenu childMenu = new ChildMenu();
                childMenu.setMenuId(permissionMenuInfo.getMenuId());
                parentMenu.setMenuId(Long.valueOf(permissionMenuInfo.getPid())).addChildMenu(childMenu);
                childMenu.addPermission(new Permission().setPermissionId(permissionMenuInfo.getPermissionId()));
                menus.put(permissionMenuInfo.getPid(), parentMenu);
            }
            if (menu != null) {
                Boolean flag = false;
                List<ChildMenu> childMenuList = menu.getChildMenuList();
                for (ChildMenu childMenu : childMenuList) {
                    if (childMenu.getMenuId() == permissionMenuInfo.getMenuId()) {
                        flag = true;
                        childMenu.addPermission(new Permission().setPermissionId(permissionMenuInfo.getPermissionId()));
                    }
                }
                if (!flag) {
                    ChildMenu childMenu = new ChildMenu();
                    childMenu.setMenuId(permissionMenuInfo.getMenuId());
                    childMenu.addPermission(new Permission().setPermissionId(permissionMenuInfo.getPermissionId()));
                    childMenuList.add(childMenu);
                }
                Menu parentMenu = new Menu();
                parentMenu.setMenuId(Long.valueOf(permissionMenuInfo.getPid())).setChildMenuList(childMenuList);
                menus.put(permissionMenuInfo.getPid(), parentMenu);
            }
        }
        list = new ArrayList<>();
        for (Long pid : menus.keySet()) {
            list.add(menus.get(pid));
        }

    }

    private class Menu {
        private Long menuId;
        private List<ChildMenu> childMenuList;

        public Long getMenuId() {
            return menuId;
        }

        public Menu setMenuId(Long menuId) {
            this.menuId = menuId;
            return this;
        }

        public List<ChildMenu> getChildMenuList() {
            return childMenuList;
        }

        public Menu setChildMenuList(List<ChildMenu> childMenuList) {
            this.childMenuList = childMenuList;
            return this;
        }

        public void addChildMenu(ChildMenu childMenu) {
            if (childMenuList == null) {
                childMenuList = new ArrayList<>();
            }
            childMenuList.add(childMenu);
        }

    }

    private class ChildMenu {
        private Long menuId;
        private List<Permission> permissionList;

        public Long getMenuId() {
            return menuId;
        }

        public ChildMenu setMenuId(Long menuId) {
            this.menuId = menuId;
            return this;
        }

        public List<Permission> getPermissionList() {
            return permissionList;
        }

        public ChildMenu setPermissionList(List<Permission> permissionList) {
            this.permissionList = permissionList;
            return this;
        }

        public void addPermission(Permission permission) {
            if (permissionList == null) {
                permissionList = new ArrayList<>();
            }
            permissionList.add(permission);
        }

    }

    private class Permission {
        private Long permissionId;

        public Long getPermissionId() {
            return permissionId;
        }

        public Permission setPermissionId(Long permissionId) {
            this.permissionId = permissionId;
            return this;
        }
    }

}
